#include "tcpsever.hpp"
#include <memory>
#include "Http.hpp"

using namespace std;

void Usege(string argv)
{
    cout << argv << " + local_port" << endl;
}


shared_ptr<Response> Login(shared_ptr<Request> req)
{
    LOG(INFO,"————————————");
    string userdata;
    if(req->Method() == "GET")
    {
        userdata = req->Args();
    }
    else if(req->Method() == "POST")
    {
        userdata = req->Text();
    }
    //处理userdata

    //fork


    //exec




    auto response = factory::BulidResponse();
    int contentsize = 0;
    HttpServer s;
    string text = s.ReadFileContent("wwwroot/5.html", &contentsize);
    response->AddStatusLine(200,"OK");
    response->AddHeader("Content-Type","text/html");
    response->AddText(text);
    return response;
}

int main(int args, char *argv[])
{
    EnableCout();
    if (args != 2)
    {
        Usege(argv[0]);
        exit(USEGE_ERROR);
    }
    uint16_t port = stoi(argv[1]);
    HttpServer http_service;

    http_service.AddHandler("/login",Login);
    unique_ptr<Tcpsever> tcpsvr = make_unique<Tcpsever>(port,bind(&HttpServer::HandleHttpRequest,&http_service,placeholders::_1));
    tcpsvr->Loop();
    return 0;
}
